<%@ include file="base.jsp"%>
<%@ page import="net.amsoft.iservice.icontact.service.IContact" %>
<%@ page import="net.amsoft.iservice.icontact.hibernate.dataObject.LoginData" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="net.amsoft.iservice.icontact.util.IContactConstants" %>
<%@ page import="net.amsoft.iservice.icontact.hibernate.IBroker" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles"%>
<%
	// Service URLs
	String issoURL = "#";
	String forwardingURL = "#";
	// Get IBroker Data
	IBroker oIBroker = IContact.getIBroker(request.getServerName());
	// Get loggedInSynonym
	LoginData oLoginData = (LoginData) session.getAttribute(IContactConstants.SESSION_LOGIN_OBJ);
	if ((oIBroker!=null) && (oLoginData!=null)){
		issoURL = oIBroker.getIssoURL() + oLoginData.getLoggedInSynonym();
		forwardingURL = oIBroker.getForwardingURL() + oLoginData.getLoggedInSynonym();
	}
%>
<html>
	<head>
	<meta content="text/html; charset=UTF-8" http-equiv="content-type">
	<title><bean:message key="title.requests" /></title>
	<link rel="stylesheet" type="text/css" href="styleSheets/iservices.css" />
	<script src="js/icontact.js"></script>
	<script>
		function submitReply(rowCount){
			 var chkBox=document.getElementById("chkID"+rowCount);
			 var textBox=document.getElementById("txtID"+rowCount);
			 var subBox=document.getElementById("subID"+rowCount);
			 document.ReplyForm.hash.value=chkBox.value+'';
			 document.ReplyForm.msg.value=textBox.value;
			 document.ReplyForm.subject.value=subBox.value;
			 document.ReplyForm.submit();
		}

		function del(){
			document.DelMsgForm.submit();
		}

		function showRequest(){
			document.LoginForm.submit();
		}

		function checkLength(id,size){
			var textBox = document.getElementById(id).value;
			if (textBox.length>size)
			{
				document.getElementById(id).value = textBox.substring(0,size);
			}
		}
	</script>
	</head>
<%
String EXPROWID=(String)request.getAttribute(IContactConstants.REQ_EXP_ROW_ID);
int tmpMaxReqCount=IContactConstants.MAX_MSGS_COUNT;
int maxReplies=IContactConstants.MAX_REPLIES_COUNT;
if (EXPROWID== null){%>
	<body onload="collapseAll();">
<%} else {%>
	<body onload="toggle_req('<%= EXPROWID%>');">
<%}%>

<center>
	<table cellspacing=25 border="0" cellpadding=0 width="800">
	<%@ include file="leftMenu.jsp" %>
		<td valign="top">
			<table width="100%" border="0" cellspacing=0 cellpadding=0>
				<tr valign="bottom">
					<td align="left" height="23px" width="100%">
						<a href="<%=issoURL%>"><img src="<%=imagePath%>/tab-noclick-isso.jpg" border="0" onmouseover="this.src='<%=imagePath%>/tab-rollover-isso.jpg'" onmouseout="this.src='<%=imagePath%>/tab-noclick-isso.jpg'" /></a><img src="<%=imagePath%>/tab-click-contact.jpg" border="0" onmouseover="this.src='<%=imagePath%>/tab-rollover-contact.jpg'" onmouseout="this.src='<%=imagePath%>/tab-click-contact.jpg'" /><a href="<%=forwardingURL%>"><img src="<%=imagePath%>/tab-noclick-forwarding.jpg" border="0" onmouseover="this.src='<%=imagePath%>/tab-rollover-forwarding.jpg'" onmouseout="this.src='<%=imagePath%>/tab-noclick-forwarding.jpg'" /></a>
					</td>
				</tr>
				<tr>
					<td>
						<table border="0" cellspacing="0" cellpadding="0" width="100%">
							<tr height="10">
								<td rowspan=3 width="10" bgcolor="#999999"><img src="<%=imagePath%>/grey-square.gif"></td>
								<td colspan=4 bgcolor="#999999" valign="top">
									<table>
										<tr height="45">
											<td width="600" bgcolor="#999999" valign="middle" align="left"><img src="<%=imagePath%>/title_contact_requests.gif" /></td>
											<td width="140" bgcolor="#999999" valign="middle" align="right">
												<html:link  action="/CheckLogin" ><img border="0" src="<%=imagePath%>/button-home.jpg" alt="home" ></html:link>
												<html:link action="/Logout" ><img border="0" src="<%=imagePath%>/button-logout.jpg" alt="logout" ></html:link>
											</td>
										</tr>
									</table>
								</td>
								<td rowspan=3 width="10" bgcolor="#999999"><img src="<%=imagePath%>/grey-square.gif"></td>
							</tr>						
							<tr>
								<td bgcolor="#CCCCCC" colspan=4>
									<table border="0" cellspacing="0" cellpadding="0" width=100%>
										<tr>
											<td>
												<table border="0" cellpadding="2" width="100%">
													<tr valign="bottom" height="36">
														<html:form action="/Requests" method="POST">
														<td><nobr><font class="bigTitle"><bean:message key="label.requests"/></font></nobr></td>
														<td>
															<bean:define id="aSynonym" name="LoginData" property="allSynonyms" />
															<html:select property="synonym" value="<%=(String)session.getAttribute(IContactConstants.SESSION_SYN)%>" styleClass="black-smalltext" >
																<logic:iterate id="syn" name="aSynonym">		
																	<option><%=syn%></option>
																</logic:iterate>								
															</html:select>
														</td>
														<td width=100%><a class="link-nounderline" href="#" ><img border="0" alt="go" src="<%=imagePath%>/button-go.jpg" onclick="showRequest();" /></a></td>
														</html:form>
													</tr>
													<tr>
														<td class="blue-smalltext" colspan="3"><html:errors/>&nbsp;</td>
													</tr>
												</table>
											</td>
										</tr>
										<tr>
											<td>
												<table bgcolor="#e8e8e8" border="0" width="100%">
													<tr background="<%=imagePath%>/top-bar.jpg" class="titleRow" valign="top">
														<td width="50"><b><bean:message key="label.select" /></b></td>
														<td width="160"><b><bean:message key="label.from" /></b></td>
														<td width="220"><b><bean:message key="label.inameOrIname" /></b></td>
														<td width="110"><b><bean:message key="label.date" /></b></td>
														<td width="50">&nbsp;</td>
													</tr>
													<%  int reqCount = 0; 
														int curPage=1+((LoginData)session.getAttribute(IContactConstants.SESSION_LOGIN_OBJ)).getCurReqIndex(); 
														pageContext.setAttribute("PREVPAGE",String.valueOf(curPage-1));
														pageContext.setAttribute("NEXTPAGE",String.valueOf(curPage+1));
														if(curPage<10){
															pageContext.setAttribute("CURPAGE",""+String.valueOf(curPage));
														}else{
															pageContext.setAttribute("CURPAGE",String.valueOf(curPage));
														}
													%>
									
													<html:form action="/Reply" method="post">
														<html:hidden property="msg" value="" />
														<html:hidden property="hash" value="" />
														<html:hidden property="subject" value="" />
													</html:form>
									
													<html:form action="/DeleteMsgs" method="Post">
													<logic:iterate id="msg" name="<%=IContactConstants.REQ_MSG_LIST %>" scope="request">
													<tr id='req<%=++reqCount%>' >
														<td><input type="checkbox" id="chkID<%=reqCount%>" name="requestsToDelete[]" value='<bean:write name="msg" property="hash" />'></td>
														<td class="blue-xsmalltext"><a style="cursor:pointer; color:#3859ad;" onclick="toggle_req('req<%=reqCount%>');"><b><bean:write name="msg" property="senderName" /></b></a></td>
														<td class="blue-xsmalltext" ><a style="cursor:pointer; color:#3859ad;" onclick="toggle_req('req<%=reqCount%>');"><b><bean:write name="msg" property="senderIName" />&nbsp;<bean:write name="msg" property="senderEMail" /></b></a></td>
														<td class="blue-xsmalltext" width="110"><b><bean:write name="msg" property="creationTime" formatKey="display.date.format" /></b></td>
														<td title='<bean:message key="label.button.reply" />' class="blue-xsmalltext" height="20px">
															<a class="link-nounderline" style="cursor:pointer; color:#3859ad;" >
															<logic:notEmpty name="msg" property="senderIName" >
																<img border="0" src="<%=imagePath%>/button-reply.jpg"  onclick="window.open('<bean:message key="contact.link" />'+'<bean:write name="msg" property="senderIName"/>','ContactPageWindow','width=800,height=750,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,copyhistory=yes,resizable=yes')">
															</logic:notEmpty>														
															<logic:present name="msg" property="senderEMail" >
															<logic:empty name="msg" property="senderIName" >
																<img border="0" src="<%=imagePath%>/button-reply.jpg" onclick="toggle_req_rep('req<%=reqCount%>','req<%=reqCount%>_rep');">
															</logic:empty>
															</logic:present>	
															</a>
														</td>
													</tr>
													<tr title="requestText" id='req<%=reqCount%>_msg' style="display:none;">
														<td class="blue-xsmalltext">&nbsp;</td>
														<td colspan="3" class="black-xsmalltext" >
															<bean:write name="msg" property="text"/>
															<%	
																String curReplyPage=(String)request.getAttribute(IContactConstants.REQ_CUR_REPLY_PAGE);
																int iCurReplyPage=Integer.parseInt(curReplyPage);
																pageContext.setAttribute("PREVREPLY",iCurReplyPage-1);
																pageContext.setAttribute("NEXTREPLY",iCurReplyPage+1);
																if(iCurReplyPage<maxReplies){
																	pageContext.setAttribute("CURREPLY",""+iCurReplyPage);
																}else{
																	pageContext.setAttribute("CURREPLY",iCurReplyPage);
																}
																String lengthRes = Integer.toString(maxReplies);
																String offsetRes;
																if(("req"+reqCount).equals(EXPROWID)){
																	offsetRes = Integer.toString((iCurReplyPage-1)*maxReplies);
																}else{
																	offsetRes = Integer.toString(0);
																}														
															%>
														</td>
														<td class="blue-smalltext">&nbsp;</td>
													</tr>
													<% int iReplyCount=0; 
														String cnt = Integer.toString(reqCount-1);%>
													<logic:iterate id="REPLYLIST" name="reqResList" offset="<%=cnt%>" length="1">
														<logic:iterate id="reply" name="REPLYLIST" offset="<%=offsetRes%>" length="<%=lengthRes%>">
															<tr id="req<%=reqCount%>_res<%=++iReplyCount%>" style="display:none;" >
																<td class="black-xsmalltext">&nbsp;</td>
																<td class="black-xsmalltext"><a style="cursor:pointer; color:#3859ad;" onclick="toggle_req_res('req<%=reqCount%>','req<%=reqCount%>_res<%=iReplyCount%>');"><b><bean:write name="reply" property="senderName" /></b></a></td>
																<td class="black-xsmalltext"><b><bean:write name="reply" property="senderIName" />&nbsp;<bean:write name="reply" property="senderEMail" /></b></td>
																<td class="black-xsmalltext"><b><bean:write name="reply" property="creationTime" formatKey="display.date.format" /></b></td>
																<td class="black-xsmalltext">&nbsp;</td>
															</tr>
															<tr id="req<%=reqCount%>_res<%=iReplyCount%>_msg" style="display:none;">
																<td class="blue-xsmalltext">&nbsp;</td>
																<td class="black-xsmalltext" colspan="3">
																	<bean:write name="reply" property="text"/>
																</td>
																<td class="blue-smalltext">&nbsp;</td>
															</tr>							
														</logic:iterate>
													</logic:iterate>
													<%	
														HashMap PREVMAP=new HashMap();
														HashMap NEXTMAP=new HashMap();
														NEXTMAP.put(IContactConstants.REQ_CUR_MSG_PAGE,pageContext.getAttribute("CURPAGE"));
														PREVMAP.put(IContactConstants.REQ_CUR_MSG_PAGE,pageContext.getAttribute("CURPAGE"));
														PREVMAP.put(IContactConstants.REQ_EXP_ROW_ID,"req"+reqCount);
														NEXTMAP.put(IContactConstants.REQ_EXP_ROW_ID,"req"+reqCount);
														if(("req"+reqCount).equals(EXPROWID)) {
															PREVMAP.put(IContactConstants.REQ_CUR_REPLY_PAGE,pageContext.getAttribute("PREVREPLY"));
															NEXTMAP.put(IContactConstants.REQ_CUR_REPLY_PAGE,pageContext.getAttribute("NEXTREPLY"));
														}else{
															PREVMAP.put(IContactConstants.REQ_CUR_REPLY_PAGE,"0");
															NEXTMAP.put(IContactConstants.REQ_CUR_REPLY_PAGE,"2");
															pageContext.setAttribute("CURREPLY","1");
														}
														pageContext.setAttribute("PREVMAP",PREVMAP);
														pageContext.setAttribute("NEXTMAP",NEXTMAP);
													%>
													<tr id="req<%=reqCount%>_res<%=++iReplyCount%>" valign="middle" style="display:none;">
														<td> &nbsp;</td>
														<td colspan="4" valign="middle" align="left">														
														<%int tmpReplyCount=Integer.parseInt(pageContext.getAttribute("CURREPLY").toString()); %>
															<logic:greaterThan value="1" name="CURREPLY">
														  		<html:link styleClass ="blue-smalltext" action="/Requests" name="PREVMAP" scope="page">
																	<bean:message key="label.newer"/>&nbsp;<
																</html:link>								
															</logic:greaterThan>							
															<bean:size id="tmpTotalReplyCount" name="REPLYLIST" />															
															<logic:greaterThan value="<%=Integer.toString(maxReplies)%>" name="tmpTotalReplyCount">
																<font class="titleRow" >
																	<b><bean:write  name="CURREPLY" scope="page" /></b>
																</font>
															</logic:greaterThan>												
															<logic:greaterThan value="<%=Integer.toString(maxReplies*tmpReplyCount)%>" name="tmpTotalReplyCount">
																<html:link styleClass ="blue-smalltext" action="/Requests" name="NEXTMAP" scope="page"  >
																	>&nbsp;<bean:message key="label.older"/>
																</html:link>
															</logic:greaterThan>
														</td>
													</tr>
													<tr id="req<%=reqCount%>_res<%=iReplyCount%>_msg" style="display:none;" >
														<td colspan=5 align=right />&nbsp;
													</tr>												
													<tr title="replyBox" id="req<%=reqCount%>_rep" style="display:none;">
														<td class="blue-xsmalltext">
															&nbsp;
														</td>
														<td class="black-xsmalltext" colspan="3">
															<table>
																<tr>
																	<td>
																		<input type="text" disabled="disabled"  size="32" value="<bean:write name="synonym" scope="session"/>" class="textbox" maxlength="30">
																		<input type="text" size="32" id="subID<%=reqCount%>" value="Re: contact request" class="textbox" maxlength="30">
																	</td>
																</tr>
																<tr>
																	<td>
																		<TEXTAREA id="txtID<%=reqCount%>" NAME="" ROWS="10" COLS="65" class="textarea-smalltext" onkeyup="checkLength('txtID<%=reqCount%>',255);" onmouseout="checkLength('txtID<%=reqCount%>',255);" ></TEXTAREA>
																	</td>
																</tr>
															</table>
														</td>
														<td title="send" class="blue-xsmalltext" height="20px" valign="top">
															<a class="link-nounderline" style="cursor:pointer; color:#3859ad;" > 
															<img border="0" alt="send" src="<%=imagePath%>/button-send.jpg" onclick="submitReply('<%=reqCount%>');" /> </a>
														</td>
													</tr>
													<tr>
														<td class="black-xsmalltext" colspan="5">
															<img height="2px" width="100%" src="<%=imagePath%>/dotted-line.gif" />
														</td>
													</tr>
												</logic:iterate>
													<tr valign="middle"  >
														<td>
															<html:link href="#" accesskey="enter" > 
																<img border="0" src="<%=imagePath%>/button-delete.jpg" 
																	onclick="del();" /> 
															</html:link>
														</td>
														<td colspan="4" valign="middle">
															<%int tmpReqCount=Integer.parseInt(pageContext.getAttribute("CURPAGE").toString());
															  int tmpTotalReqCount=Integer.parseInt(session.getAttribute(IContactConstants.SESSION_REQ_COUNT).toString());
															  if(tmpReqCount > 1){
															%>															
																<html:link action="/Requests" paramId="<%=IContactConstants.REQ_CUR_MSG_PAGE%>" paramName="PREVPAGE" paramScope="page" >
																	<img border="0" align="middle" alt="Previous" src="<%=imagePath%>/button-page-previous.jpg" /> 
																</html:link>
															<%} if(tmpTotalReqCount > tmpMaxReqCount){
															%>
																<font class="titleRow" >
																	<b><bean:write  name="CURPAGE" scope="page" /></b>
																</font>
															<%} if(tmpTotalReqCount > tmpMaxReqCount*tmpReqCount){ %>	
																<html:link action="/Requests" paramId="<%=IContactConstants.REQ_CUR_MSG_PAGE%>" paramName="NEXTPAGE"  paramScope="page">
																	<img border="0" align="middle"  alt="Next" src="<%=imagePath%>/button-page-next.jpg"  />
																</html:link>
															<%} %>	
														</td>
													</tr>
											</html:form>
										</table>
									</td>
								</tr>
							</table>
						</td>
					</tr>						
					<tr height="10">
						<td width="585" bgcolor="#999999" colspan=4>&nbsp;</td>
					</tr>						
				</table>
			</td>
		</tr>
	</table>
</center>
</body>
</html>
